package com.apollo.android.um.imp;

import com.apollo.android.um.anno.register.DeviceToken;
import com.apollo.android.um.anno.register.DeviceTokenError;
import com.apollo.android.um.constants.Constants;
import com.apollo.android.um.utils.AppCache;
import com.apollo.android.um.utils.LogUtils;
import com.apollo.android.utils.Helper;
import com.apollo.android.utils.ResUtils;
import com.umeng.message.IUmengRegisterCallback;

import java.lang.reflect.Method;

public class URegisterImp implements IUmengRegisterCallback {

    @Override
    public void onSuccess(String deviceToken) {
        LogUtils.log("注册成功 deviceToken " + deviceToken);
        AppCache.saveDeviceToken(deviceToken);
        final String registerClassName = ResUtils.getString(Constants.CLASS_DEVICE_REGISTER);
        if (Helper.isNotEmpty(registerClassName)) {
            try {
                Class registerClass = Class.forName(registerClassName);
                Method[] methods = registerClass.getDeclaredMethods();
                if (methods != null) {
                    for (Method method : methods) {
                        try {
                            DeviceToken deviceTokenAnnotation = method.getAnnotation(DeviceToken.class);
                            if (deviceTokenAnnotation != null) {
                                Class[] paramTypes = method.getParameterTypes();
                                if (paramTypes != null && paramTypes.length == 1 && paramTypes[0] == String.class) {
                                    method.invoke(null, deviceToken);
                                    break;
                                }
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onFailure(String deviceToken, String msg) {
        LogUtils.log("注册失败 deviceToken " + deviceToken + " 错误信息 " + msg);
        AppCache.saveDeviceToken(null);
        AppCache.saveDeviceTokenErrorMessage(msg);
        final String registerClassName = ResUtils.getString(Constants.CLASS_DEVICE_REGISTER);
        if (Helper.isNotEmpty(registerClassName)) {
            try {
                Class registerClass = Class.forName(registerClassName);
                Method[] methods = registerClass.getDeclaredMethods();
                if (methods != null) {
                    for (Method method : methods) {
                        try {
                            DeviceTokenError deviceTokenAnnotation = method.getAnnotation(DeviceTokenError.class);
                            if (deviceTokenAnnotation != null) {
                                Class[] paramTypes = method.getParameterTypes();
                                if (paramTypes != null && paramTypes.length == 2 && paramTypes[0] == String.class && paramTypes[1] == String.class) {
                                    method.invoke(null, deviceToken, msg);
                                    break;
                                }
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
